package com.ruoyi.project.module.wtc.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.project.domain.YhAppointment;
import com.ruoyi.project.service.IYhAppointmentService;
import com.ruoyi.project.domain.SysUserRole;
import com.ruoyi.project.service.ISysRoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getUsername;
import static com.ruoyi.common.security.utils.SecurityUtils.getUserId;
//import static com.ruoyi.common.security.utils.SecurityUtils.getNickName;


/**
 * 预约信息Controller
 *
 * @author ruoyi
 * @date 2023-03-07
 */
@RestController
@RequestMapping("/wtc/appointment")
public class WtcYhAppointmentController extends BaseController {
    @Autowired
    private IYhAppointmentService yhAppointmentService;

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 查询预约信息列表
     */
    //@RequiresPermissions("wtc:appointment:list")
    @GetMapping("/list")
    public TableDataInfo list(YhAppointment yhAppointment) {

        // 获取当前用户信息
        //String username = getNickName();
        String userId = getUsername();
        //System.out.println(username);
        //System.out.println(userId);
        //if (userId != 1 || !username.equals("admin")) {
        if (!userId.equals("admin")) {
            yhAppointment.setReceiverId(userId);
            //yhAppointment.setReceiverName(username);
        }

        startPage();
        List<YhAppointment> list = yhAppointmentService.selectYhAppointmentList(yhAppointment);
        //for (int i=0;i<list.size();i++){
        //
        //    if(list.get(i).getEquipmentType().equals("001")){
        //        list.get(i).setEquipmentType("成分分析");
        //    }else if(list.get(i).getEquipmentType().equals("002")){
        //        list.get(i).setEquipmentType("微区分析");
        //    }else if(list.get(i).getEquipmentType().equals("003")){
        //        list.get(i).setEquipmentType("感官分析");
        //    }else if(list.get(i).getEquipmentType().equals("004")){
        //        list.get(i).setEquipmentType("物性分析");
        //    }else if(list.get(i).getEquipmentType().equals("005")){
        //        list.get(i).setEquipmentType("分子生物学分析");
        //    }else if(list.get(i).getEquipmentType().equals("006")){
        //        list.get(i).setEquipmentType("微生物分析");
        //    }else if(list.get(i).getEquipmentType().equals("007")){
        //        list.get(i).setEquipmentType("常规分析");
        //    }else if(list.get(i).getEquipmentType().equals("008")){
        //        list.get(i).setEquipmentType("前处理仪器设备");
        //    }else if(list.get(i).getEquipmentType().equals("009")){
        //        list.get(i).setEquipmentType("离心分离分析");
        //    }else if(list.get(i).getEquipmentType().equals("010")){
        //        list.get(i).setEquipmentType("分析软件");
        //    }else if(list.get(i).getEquipmentType().equals("011")){
        //        list.get(i).setEquipmentType("其他");
        //    }else if(list.get(i).getEquipmentType().equals("012")){
        //        list.get(i).setEquipmentType("会议室");
        //    }
        //}
        return getDataTable(list);
    }

    /**
     * 导出预约信息列表
     */
    //@RequiresPermissions("wtc:appointment:export")
    @Log(title = "预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhAppointment yhAppointment) {
        List<YhAppointment> list = yhAppointmentService.selectYhAppointmentList(yhAppointment);
        ExcelUtil<YhAppointment> util = new ExcelUtil<YhAppointment>(YhAppointment.class);
        util.exportExcel(response, list, "预约信息数据");
    }

    /**
     * 获取预约信息详细信息
     */
    //@RequiresPermissions("wtc:appointment:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(yhAppointmentService.selectYhAppointmentById(id));
    }

    /**
     * 新增预约信息
     */
    //@RequiresPermissions("wtc:appointment:add")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhAppointment yhAppointment) {
        return toAjax(yhAppointmentService.insertYhAppointment(yhAppointment));
    }

    /**
     * 修改预约信息
     */
    //@RequiresPermissions("wtc:appointment:edit")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhAppointment yhAppointment) {
        //System.out.println(yhAppointment);
        return toAjax(yhAppointmentService.updateYhAppointment(yhAppointment));
    }

    /**
     * 删除预约信息
     */
    //@RequiresPermissions("wtc:appointment:remove")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(yhAppointmentService.deleteYhAppointmentByIds(ids));
    }

    /**
     * 获取用户权限信息
     */
    @GetMapping("/roleId")
    public Long roleId() {
        // 获取当前用户信息
        Long userId = getUserId();

        SysUserRole sysRole = sysRoleService.selectRoleByUser(userId);
        Long roleId = sysRole.getRoleId();
        //System.out.println(roleId);

        return roleId;
    }

    /**
     * 发送企业微信通知
     */
    @GetMapping("/message/{id}")
    public void sendMessage(@PathVariable("id") Long id) {
        System.out.println(id);
        YhAppointment yhAppointment = yhAppointmentService.selectYhAppointmentById(id);

        //System.out.println(yhAppointment);

        //String userId = yhAppointment.getStudentId();
        //String username = yhAppointment.getName();
        //String result = yhAppointment.getAuditStatus();
        //
        //System.out.println(userId);
        //System.out.println(username);
        //System.out.println(result);

        //WtcWeChatMassage.sendWeChatMassage(userId,username,result);
        WtcWeChatMassage.sendWeChatMassage(yhAppointment);
    }

    /**
     * 获取审核用户的roleId
     */
    //@GetMapping("/roleId/{studentId}")
    //public Long getRole(@PathVariable("studentId") String studentId) {
    //
    //}
}
